const app = getApp();
import util from "../../utils/util";
Component({
    /**
     * 组件的属性列表
     */
    properties: {
        action: {
            value: "",
            type: String,
        },
        city: {
            value: "",
            type: String,
        },
        text: {
            value: "",
            type: String,
        },
        name: {
            value: "",
            type: String
        },
        tip: {
            value: "订阅消息成功",
            type: String 
        }
    },

    /**
     * 组件的初始数据
     */
    data: {

    },

    /**
     * 组件的方法列表
     */
    methods: {

        // 订阅消息
        subscribe() {
            if (this.properties.text == "已订阅")
                return;
            if (wx.getStorageSync("openid")) {
                const tepId = "Us5cSgjk6oFqj6UxBMaob9pUbEKTXD4D15cyINwtCPM";
                wx.requestSubscribeMessage({
                    tmplIds: [tepId],
                    success: res => {
                        wx.getSetting({
                            withSubscriptions: true,
                            success: res => {
                                
                            },
                            fail: () => {},
                            complete: () => {}
                        });
                        if (res[tepId] === "accept") {
                            let data = {
                                action: "msg",
                                openid: wx.getStorageSync("openid"),
                                city: this.properties.city,
                                name: util.getName(this.properties.action) ? util.getName(this.properties.action) : this.properties.name,
                            };
                            util.request(`sub_message.php`, data, "POST").then(res => {
                                wx.showToast({
                                    title: this.properties.tip,
                                    icon: 'none',
                                    duration:2500
                                });
                                this.triggerEvent("subsuccess")

                                // 统一上报函数
                                util.reportMessage("dingyueliang", { action: this.properties.name });
                            });
                        }
                    },
                });
            }
        },
    }
})